﻿@model IEnumerable<BaseData.Model.Project>
@{
    ViewBag.Title = "项目管理";
    var idx = 0;
}

<div>
    <ol class="breadcrumb">
        <li><a href="../Projects/Index">项目管理</a></li>
        <li><a href="../Departments/Index">部门管理</a></li>
        <li><a href="../Stations/Index">点位管理</a></li>
    </ol>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <table width="100%">
                    <tr>
                        <td>
                            <h2 class="panel-title">项目管理</h2>
                        </td>
                        <td class="pull-right">

                            <a href="#" data-toggle="modal" data-backdrop="static" data-target="#myModal" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span> 新建</a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="panel-body">
                <table class="table table-striped">
                    <tr>
                        <th>
                            序号
                        </th>
                        <th>
                            项目名称
                        </th>
                        <th>
                            项目类型
                        </th>
                        <th>
                            操作
                        </th>
                    </tr>

                    @foreach (var item in Model)
                    {
                        idx++;
                        <tr>
                            <td>
                                @idx
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.ProjectName)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.ProjectType.ProjectTypeName)
                            </td>
                            <td>
                                <a title="编辑" href="javascript:Edit('@item.ProjectID');"><span class=" glyphicon glyphicon-edit"></span></a>
                                &nbsp;&nbsp;
                                <a title="删除" href="javascript:Delete('@item.ProjectID')"><span class="glyphicon glyphicon-trash"></span></a>
                                @*&nbsp;&nbsp;
                                    <a title="添加部门" href="javascript:Add();"><span class="glyphicon glyphicon-plus-sign"></span></a>*@
                            </td>
                        </tr>
                    }

                </table>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">项目新建</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">
                    <div class="form-group">
                        <label for="projectname" class="col-sm-2 control-label">项目名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="projectname"
                                   placeholder="请输入项目名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="projecttype" class="col-sm-2 control-label">项目类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="projecttype">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
         
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="errmsg">输出数据不合法，请检查重新输入！</div>
                <button id="save" type="button" class="btn btn-default btn-sm">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myEditModal" tabindex="-1" role="dialog" aria-labelledby="myEditModalLable" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myEditModalLable">项目修改</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">
                    <div class="form-group">
                        <label for="Eprojectname" class="col-sm-2 control-label">项目名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="Eprojectname"
                                   placeholder="请输入项目名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="eprojecttype" class="col-sm-2 control-label">项目类型</label>
                        <div class="col-sm-10">
                            <select class="form-control" id="eprojecttype">
                                <option value="">默认选择</option>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="ederrmsg">输出数据不合法，请检查重新输入！</div>
                <button id="esave" type="button" class="btn btn-default btn-sm">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
                <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
            </div>
        </div>
    </div>
</div>


@section Scripts {
    <script>
        $(function () {
            $.getJSON("/Projects/GetProjectType", function (data) {
                var item = "<option value=-1>=请选择=</option>";
                for (var i = 0; i < data.length; i++) {
                    item += ' <option value="' + data[i].ProjectTypeID + '">' + data[i].ProjectTypeName + '</option>';
                }
                $("#projecttype").empty().append(item);
                $("#eprojecttype").empty().append(item);
            });

            $("#projecttype").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#eprojecttype").bind("change", function () {
                $(this).parents(".form-group").attr("class", "form-group");
            });

            $("#save").bind("click", function () {
                var model = {};
                model.ProjectTypeID = $("#projecttype").val();
                model.ProjectName = $("#projectname").val();
                if (model.ProjectName == "") {
                    $("#projectname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.ProjectTypeID == "-1") {
                    $("#projecttype").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/Projects/Create",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/Projects/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status==500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });
        });
        function Edit(id) {
            $.ajax({
                url: "/Projects/Edit",
                type: 'GET',
                data: { id: id },
                success: function (data) {
                    $("#Eprojectname").val(data.ProjectName);
                    $("#eprojecttype").val(data.ProjectTypeID);
                    $('#myEditModal').modal('show');
                }
            });

            $("#esave").bind("click", function () {
                var model = {};
                model.ProjectID = id;
                model.ProjectName = $("#Eprojectname").val();
                model.ProjectTypeID = $("#eprojecttype").val();
                if (model.ProjectName == "") {
                    $("#Eprojectname").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.ProjectTypeID == "-1") {
                    $("#eprojecttype").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/Projects/Edit",
                    type: 'POST',
                    data: { project: JSON.stringify(model) },
                    success: function (data) {
                        location.href = "/Projects/Index";
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#ederrmsg").css("display", "block");
                        }
                    }
                });
            });
        }

        function Delete(obj) {
            if (confirm("确定要删除?")) {
                $.ajax({
                    url: "/Projects/Delete",
                    type: 'GET',
                    data: { id: obj },
                    success: function (data) {
                        location.href = "/Projects/Index";
                    }
                });
            }
        }
    </script>
}
